package com.itheima.hmshopping.app.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.SmsCouponServiceApi;
import com.itheima.hmshopping.dto.SmsCouponDto;
import com.itheima.hmshopping.dubbo.api.SmsCouponProductCategoryRelationServiceApi;
import com.itheima.hmshopping.dubbo.api.SmsCouponProductRelationApi;
import com.itheima.hmshopping.entity.SmsCoupon;
import com.itheima.hmshopping.entity.SmsCouponProductCategoryRelation;
import com.itheima.hmshopping.entity.SmsCouponProductRelation;
import com.itheima.hmshopping.exceptionhander.CustomException;
import com.itheima.hmshopping.util.HmPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SmsCouponService {

    @DubboReference
    private SmsCouponServiceApi smsCouponServiceApi;

    @DubboReference
    private SmsCouponProductCategoryRelationServiceApi smsCouponProductCategoryRelationServiceApi;

    @DubboReference
    private SmsCouponProductRelationApi smsCouponProductRelationApi;


    //根据优惠券名称和类型分页获取优惠券列表
    public HmPage list(String name, Integer pageNum, Integer pageSize, String type) {
        Page page=smsCouponServiceApi.list(name,pageNum,pageSize,type);
        HmPage hmPage=new HmPage();
        //设置总页数
        hmPage.setTotalPage(Convert.toInt(page.getPages()));
        //查询数据
        hmPage.setList(page.getRecords());
        //第几页
        hmPage.setPageNum(Convert.toInt(pageNum));
        //总页数
        hmPage.setTotal(Convert.toInt(page.getTotal()));
        //每页条数
        hmPage.setPageSize(pageSize);
        return hmPage;
    }


    //添加优惠券
    public void insert(SmsCouponDto smsCouponDto) {
        if (ObjectUtil.isEmpty(smsCouponDto)){
            throw new CustomException("对象不能为空");
        }
        //封装smsCoupon并新增
        SmsCoupon smsCoupon=new SmsCoupon();
        //复制smsCoupon
        BeanUtils.copyProperties(smsCouponDto,smsCoupon);
        //新增优惠卷
        smsCouponServiceApi.insert(smsCoupon);
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponDto.getProductCategoryRelationList();
        //新增优惠券和产品分类关系表
        smsCouponProductCategoryRelationServiceApi.insertList(productCategoryRelationList);
        //优惠券和产品的关系表
        List<SmsCouponProductRelation> productRelationList = smsCouponDto.getProductRelationList();
        smsCouponProductRelationApi.insertLists(productRelationList);
    }


    //删除优惠卷
    public void delete(Integer id) {
        //删除优惠卷及相关数据
        smsCouponProductRelationApi.delete(id);
        smsCouponServiceApi.delect(id);
        smsCouponProductCategoryRelationServiceApi.delect(id);

    }

    //获取单个优惠券的详细信息
    public SmsCouponDto selectById(Integer id) {
        //查询三张表
        List<SmsCouponProductCategoryRelation> smsCouponProductCategoryRelation=smsCouponProductCategoryRelationServiceApi.selectById(id);
        SmsCoupon smsCoupon=smsCouponServiceApi.selectById(id);
        List<SmsCouponProductRelation> smsCouponProductRelations=smsCouponProductRelationApi.selectById(id);
        SmsCouponDto smsCouponDto=new SmsCouponDto();
        BeanUtils.copyProperties(smsCoupon,smsCouponDto);
        smsCouponDto.setProductCategoryRelationList(smsCouponProductCategoryRelation);
        smsCouponDto.setProductRelationList(smsCouponProductRelations);
        return smsCouponDto;
    }

    //修改优惠卷
    public void update(SmsCouponDto smsCouponDto,Integer id) {
        SmsCoupon smsCoupon=new SmsCoupon();
        BeanUtils.copyProperties(smsCouponDto,smsCoupon);
        //更新优惠劵
        smsCouponServiceApi.update(smsCoupon);
        //获取优惠劵id
        Long couponDtoId = smsCouponDto.getId();
        Integer couponId = Convert.toInt(couponDtoId);
        //新增
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponDto.getProductCategoryRelationList();
        if (ObjectUtil.isNotEmpty(productCategoryRelationList)){
            //删除SmsCouponProductCategoryRelation对应的数据
            smsCouponProductCategoryRelationServiceApi.delect(couponId);
            //设置cid
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelation.setCouponId(couponDtoId);
            }
            //新增SmsCouponProductCategoryRelation对应的数据
            smsCouponProductCategoryRelationServiceApi.insertList(productCategoryRelationList);
        }
        List<SmsCouponProductRelation> productRelationList = smsCouponDto.getProductRelationList();
        if (ObjectUtil.isNotEmpty(productRelationList)){
            //先删除SmsCouponProductRelation的数据
            smsCouponProductRelationApi.delete(couponId);
            //修改cid
            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelation.setCouponId(couponDtoId);
            }
            //新增
            smsCouponProductRelationApi.insertLists(productRelationList);
        }
    }
}
